package com.yipao.db.domain;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * 管理员
 *
 * @author tianhao
 */
@Data
@ToString
@EqualsAndHashCode
public class YipaoAdmin implements java.io.Serializable {

    private Integer id;
    private String username;
    private String password;
    private String lastLoginIp;
    private LocalDateTime lastLoginTime;
    /**
     * 头像地址
     */
    private String avatar;
    private LocalDateTime addTime;
    private LocalDateTime updateTime;
    private Boolean deleted;
    private Integer[] roleIds;
    /**
     * 手机号码
     */
    private String mobile;
    private String weixinOpenid;

    public enum Column {
        id("id", "id", "INTEGER", false),
        mobile("mobile", "mobile", "VARCHAR", false),
        username("username", "username", "VARCHAR", false),
        password("password", "password", "VARCHAR", true),
        lastLoginIp("last_login_ip", "lastLoginIp", "VARCHAR", false),
        lastLoginTime("last_login_time", "lastLoginTime", "TIMESTAMP", false),
        avatar("avatar", "avatar", "VARCHAR", false),
        weixinOpenId("weixin_openid", "weixinOpenId", "VARCHAR", false),
        addTime("add_time", "addTime", "TIMESTAMP", false),
        updateTime("update_time", "updateTime", "TIMESTAMP", false),
        deleted("deleted", "deleted", "BIT", false),
        roleIds("role_ids", "roleIds", "VARCHAR", false);

        private static final String BEGINNING_DELIMITER = "`";
        private static final String ENDING_DELIMITER = "`";
        private final String column;
        private final boolean isColumnNameDelimited;
        private final String javaProperty;
        private final String jdbcType;


        public String value() {
            return this.column;
        }


        public String getValue() {
            return this.column;
        }


        public String getJavaProperty() {
            return this.javaProperty;
        }


        public String getJdbcType() {
            return this.jdbcType;
        }


        Column(String column, String javaProperty, String jdbcType, boolean isColumnNameDelimited) {
            this.column = column;
            this.javaProperty = javaProperty;
            this.jdbcType = jdbcType;
            this.isColumnNameDelimited = isColumnNameDelimited;
        }


        public String desc() {
            return this.getEscapedColumnName() + " DESC";
        }


        public String asc() {
            return this.getEscapedColumnName() + " ASC";
        }


        public static Column[] excludes(Column... excludes) {
            ArrayList<Column> columns = new ArrayList<>(Arrays.asList(Column.values()));
            if (excludes != null && excludes.length > 0) {
                columns.removeAll(new ArrayList<>(Arrays.asList(excludes)));
            }
            return columns.toArray(new Column[]{});
        }


        public String getEscapedColumnName() {
            if (this.isColumnNameDelimited) {
                return new StringBuilder().append(BEGINNING_DELIMITER).append(this.column).append(ENDING_DELIMITER).toString();
            } else {
                return this.column;
            }
        }


        public String getAliasedEscapedColumnName() {
            return this.getEscapedColumnName();
        }
    }
}
